package com.xzc.jcip._2_.线程安全性问题;

import net.jcip.annotations.GuardedBy;
import net.jcip.annotations.ThreadSafe;

import static java.lang.Thread.sleep;

@ThreadSafe
public class Sequence {
    @GuardedBy("this")
    private int Value;

    public synchronized int getNext() {
        return Value++;
    }

    public static void main(String[] args) {
        Sequence sequence = new Sequence();
        for (int i = 0; i < 3; i++) {
            int finalI = i;
            new Thread(() -> {
                while (true) {
                    System.out.println("t" + finalI + "=>" + sequence.getNext());
                    try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
